<html>
<head>
<script src="../OLLoader.js"></script>
  <script type="text/javascript">

    var geometry = null, node = null;
    
    function test_SVG_constructor(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(1);
        var r = new OpenLayers.Renderer.SVG(document.body);
        t.ok(r instanceof OpenLayers.Renderer.SVG, "new OpenLayers.Renderer.SVG returns SVG object" );
    }
    
    function test_SVG_destroy(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(1);
        
        var g_Destroy = false;
        
        OpenLayers.Renderer.Elements.prototype._destroy = 
            OpenLayers.Renderer.Elements.prototype.destroy;
            
        OpenLayers.Renderer.prototype.destroy = function() {
            g_Destroy = true;
        }
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.destroy();
        
        t.eq(g_Destroy, true, "OpenLayers.Renderer.Elements.destroy() called");
        
        OpenLayers.Renderer.prototype.destroy = 
            OpenLayers.Renderer.prototype._destroy;
    }
    
    function test_SVG_setextent(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(5);
        
        OpenLayers.Renderer.Elements.prototype._setExtent =
            OpenLayers.Renderer.Elements.prototype.setExtent;
        
        var g_SetExtent = false;
        OpenLayers.Renderer.Elements.prototype.setExtent = function() {
            g_SetExtent = true;
        }
                
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.setSize(new OpenLayers.Size(4,4));
        r.map = {
            getResolution: function() {
                return 0.5;
            }
        }
        
        var extent = new OpenLayers.Bounds(1,2,3,4);
        r.setExtent(extent);
        
        t.eq(g_SetExtent, true, "Elements.setExtent() called");
        
        t.eq(r.left, -2, "left is correct");
        t.eq(r.top, 8, "top is correct");
        
        t.eq(r.rendererRoot.getAttributeNS(null, "viewBox"), "0 0 4 4", "rendererRoot viewBox is correct");
        
        // test extent changes
        var extent = new OpenLayers.Bounds(4,3,2,1);
        r.setExtent(extent);
        var el = r.createNode("g");
        el.setAttributeNS(null, "transform", "translate(-6,-6)");
        t.eq(r.root.getAttributeNS(null, "transform"), el.getAttributeNS(null, "transform"), "rendererRoot viewBox is correct after a new setExtent");

        OpenLayers.Renderer.Elements.prototype.setExtent =
            OpenLayers.Renderer.Elements.prototype._setExtent;
    }
    
    function test_SVG_setsize(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(2);
                
        var r = new OpenLayers.Renderer.SVG(document.body);
        
        var size = new OpenLayers.Size(1,2);
        r.setSize(size);
        t.eq(r.rendererRoot.getAttributeNS(null, "width"), size.w.toString(), "width is correct");
        t.eq(r.rendererRoot.getAttributeNS(null, "height"), size.h.toString(), "height is correct");
    }
    
    function test_SVG_drawpoint(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
    
        t.plan(1);

        var r = new OpenLayers.Renderer.SVG(document.body);
        
        var properDraw = false;
        var g_Radius = null;
        r.drawCircle = function(n, g, r) {
            properDraw = true;
            g_Radius = 1;
        }
        r.drawPoint();
        
        t.ok(properDraw && g_Radius == 1, "drawPoint called drawCircle with radius set to 1");
    }
    
    function test_SVG_drawcircle(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(5);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.resolution = 0.5;
        r.left = 0;
        r.top = 0;
        
        var node = document.createElement('div');
        
        var geometry = {
            x: 1,
            y: 2
        }
        
        r.drawCircle(node, geometry, 3);
        
        t.eq(node.getAttributeNS(null, 'cx'), '2', "cx is correct");
        t.eq(node.getAttributeNS(null, 'cy'), '-4', "cy is correct");
        t.eq(node.getAttributeNS(null, 'r'), '3', "r is correct");
       
        // #1274: out of bound node fails when first added
        var geometry = {
            x: 10000000,
            y: 200000000,
            CLASS_NAME: "OpenLayers.Geometry.Point",
            id: "foo",
            getBounds: function() {return {bottom: 0}}
        }
        node.id = geometry.id;
        r.root.appendChild(node);

        var drawCircleCalled = false;
        r.drawCircle = function() {
            drawCircleCalled = true;
            return OpenLayers.Renderer.SVG.prototype.drawCircle.apply(r, arguments);
        }
        
        r.drawGeometry(geometry, {pointRadius: 3}, "blah_4000");
        t.eq(drawCircleCalled, true, "drawCircle called on drawGeometry for a point geometry.")
        t.ok(node.parentNode != r.root, "circle will not be drawn when coordinates are outside the valid range");
    }
    
    function test_SVG_drawlinestring(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(2);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        
        var node = document.createElement('div');
        
        var geometry = {
            components: "foo"
        }
        g_GetString = false;
        g_Components = null;
        r.getComponentsString = function(c) {
            g_GetString = true;
            g_Components = c;
            return {path: "bar", complete: true};
        }
        
        r.drawLineString(node, geometry);
        
        t.ok(g_GetString && g_Components == "foo", "getComponentString is called with valid arguments");
        t.eq(node.getAttributeNS(null, "points"), "bar", "points attribute is correct");
    }
    
    function test_SVG_drawlinearring(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(2);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        
        var node = document.createElement('div');
        
        var geometry = {
            components: "foo"
        }
        g_GetString = false;
        g_Components = null;
        r.getComponentsString = function(c) {
            g_GetString = true;
            g_Components = c;
            return {path: "bar", complete: true};
        }
        
        r.drawLinearRing(node, geometry);
        
        t.ok(g_GetString, "getComponentString is called with valid arguments");
        t.eq(node.getAttributeNS(null, "points"), "bar", "points attribute is correct");
    }

    function test_SVG_drawpolygon(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(4);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        
        var node = document.createElement('div');
        
        var linearRings = [{
            components: ["foo"]
        },{
            components: ["bar"]
        }]
        
        var geometry = {
            components: linearRings
        }
        g_GetString = false;
        r.getShortString = function(c) {
            g_GetString = true;
            return c;
        }
        
        r.drawPolygon(node, geometry);
        
        t.ok(g_GetString, "getShortString is called");
        t.eq(node.getAttributeNS(null, "d"), " M foo M bar z", "d attribute is correctly set");
        t.eq(node.getAttributeNS(null, "fill-rule"), "evenodd", "fill-rule attribute is correctly set");
        
        r.getShortString = function(c) {
            return false;
        }
        t.eq(r.drawPolygon(node, geometry), false, "drawPolygon returns false if one linearRing cannot be drawn");
    }

    function test_SVG_drawrectangle(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(4);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.resolution = 0.5;
        r.left = 0;
        r.top = 0;
        
        var node = document.createElement('div');
        
        var geometry = {
            x: 1,
            y: 2,
            width: 3,
            height: 4
        }
        
        r.drawRectangle(node, geometry);
        
        t.eq(node.getAttributeNS(null, "x"), "2", "x attribute is correctly set");
        t.eq(node.getAttributeNS(null, "y"), "-4", "y attribute is correctly set");
        t.eq(node.getAttributeNS(null, "width"), "6", "width attribute is correctly set");
        t.eq(node.getAttributeNS(null, "height"), "8", "height attribute is correctly set");
    }
    
    function test_SVG_getcomponentsstring(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(1);
        
        var components = ['foo', 'bar'];
        
        OpenLayers.Renderer.SVG.prototype._getShortString = 
            OpenLayers.Renderer.SVG.prototype.getShortString;
            
        OpenLayers.Renderer.SVG.prototype.getShortString = function(p) {
            return p;
        };
        
        var string = OpenLayers.Renderer.SVG.prototype.getComponentsString(components).path;
        t.eq(string, "foo,bar", "returned string is correct");
        
        OpenLayers.Renderer.SVG.prototype.getShortString = 
            OpenLayers.Renderer.SVG.prototype._getShortString;
    }
    
    
    
    function test_SVG_getshortstring(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(1);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.resolution = 0.5;
        r.left = 0;
        r.top = 0;
        
        var point = {
            x: 1,
            y: 2
        };
        
        var string = r.getShortString(point);
        t.eq(string, "2,-4", "returned string is correct");
    }
        
    function test_svg_importsymbol(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }

        t.plan(2);
        
        var r = new OpenLayers.Renderer.SVG(document.body);

        r.importSymbol("square");

        var polygon = document.getElementById(r.container.id + "_defs").firstChild.firstChild;
        
        var pass = false;
        for (var i = 0; i < polygon.points.numberOfItems; i++) {
            var p = polygon.points.getItem(i);
            pass = p.x === OpenLayers.Renderer.symbol.square[2*i] && 
                   p.y === OpenLayers.Renderer.symbol.square[2*i+1];
            if (!pass) {
                break;
            }
        }
        t.ok(pass, "Square symbol rendered correctly");
        t.ok(r.symbolMetrics["-square"], "Symbol metrics cached correctly.");
    }
        
    function test_svg_dashstyle(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }

        t.plan(5);
        
        var r = new OpenLayers.Renderer.SVG(document.body);

        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dot"}, 1), "1,4", "dot dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dash"}, 1), "4,4", "dash dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "longdash"}, 1), "8,4", "longdash dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dashdot"}, 1), "4,4,1,4", "dashdot dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "longdashdot"}, 1), "8,4,1,4", "dashdot dasharray created correctly");
    }
    
    function test_svg_clipline(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(3);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.setSize(new OpenLayers.Size(0, 0));
        r.map = {
            getResolution: function() {
                return 0.5;
            }
        }
        r.setExtent(new OpenLayers.Bounds(0, 0, 0, 0));

        var node = document.createElement('div');

        var geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(0, -5000),
            new OpenLayers.Geometry.Point(10000, 0),
            new OpenLayers.Geometry.Point(0, 5000)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "0,10000,15000,2500,15000,-2500,0,-10000", "Line with 3 points correctly clipped at inValidRange bounds");
        
        geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(0, -5000),
            new OpenLayers.Geometry.Point(10000, 0)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "0,10000,15000,2500", "2-point line with 2nd point outside range correctly clipped at inValidRange bounds");

        var geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(10000, 0),
            new OpenLayers.Geometry.Point(0, 5000)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "15000,-2500,0,-10000", "2-point line with 1st point outside range correctly clipped at inValidRange bounds");
    }

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
